package objects.visualization;

import base.Graphics;
import java.util.ArrayList;
import java.util.List;

public class ListBlock extends Block {
	public boolean isVertical;
	public List<Block> children = new ArrayList<Block>();
	public boolean enframe;
	
	public ListBlock( boolean isVertical, boolean enframe ) {
		this.isVertical = isVertical;
		this.enframe = enframe;
	}
	
	@Override
	public void draw( int drawingX, int drawingY ) {
		super.draw( drawingX, drawingY );
		if( enframe ) {
			Graphics.setColor( 0.25f, 0.25f, 0.5f );
			Graphics.drawEmptyRectangle( drawingX + x, drawingY + y, width, height );
		}
		for( Block block : children ) block.draw( drawingX, drawingY );
	}
	
	@Override
	public void update() {
		width = 0;
		height = 0;
		int xx = x + ( enframe ? Block.defaultMargin : 0 );
		int yy = y + ( enframe ? Block.defaultMargin : 0 );
		for ( Block childBlock : children ) {
			childBlock.x = xx;
			childBlock.y = yy;
			childBlock.update();
			if( isVertical ) {
				yy += childBlock.height;
				width = Math.max( width, childBlock.width );
				height += childBlock.height;
			} else {
				xx += childBlock.width;
				width += childBlock.width;
				height = Math.max( height, childBlock.height );
			}
			children.add( childBlock );
		}
		
		for ( Block childBlock : children ) {
			if( isVertical ) {
				childBlock.width = width;
			} else {
				childBlock.height = height;
			}
		}
		
		width += 2 * ( enframe ? Block.defaultMargin : 0 );
		height += 2 * ( enframe ? Block.defaultMargin : 0 );
	}

	public void add( Block block ) {
		children.add( block );
	}
}